<?php

namespace app\models;

use app\config\Constants;
use Lcobucci\JWT\Token;
use Yii;
use yii\base\NotSupportedException;
use yii\web\IdentityInterface;
use yii\web\UnauthorizedHttpException;

/**
 * This is the model class for table "admin".
 *
 * @property int $admin_id 自增ID
 * @property string $username 用户名
 * @property string $password 密码
 * @property string $add_time 创建日期
 */
class Admin extends \yii\db\ActiveRecord implements IdentityInterface
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'admin';
    }

    public static function findIdentity($id)
    {
        return static::findOne($id);
    }

    public static function findIdentityByAccessToken($token, $type = null)
    {
        if($token->getClaim("credential") != Constants::ADMIN){
            throw new UnauthorizedHttpException(Yii::t("app","令牌凭证错误"));
        }
        return self::findIdentity($token->getClaim("uid"));
    }

    public function getId()
    {
        return $this->admin_id;
    }

    public function getAuthKey()
    {
        throw new NotSupportedException("不支持该方法");
    }

    public function validateAuthKey($authKey)
    {
        throw new NotSupportedException("不支持该方法");
    }
}
